package com.malty.apimanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.malty.apimanage.domain.dto.api.ApiAddReq;
import com.malty.apimanage.domain.dto.api.ApiQueryReq;
import com.malty.apimanage.domain.dto.api.ApiUpdateReq;
import com.malty.apimanage.domain.vo.ApiMetaDataVO;
import com.malty.common.ErrorCode;
import com.malty.common.domain.entity.ApiMetaData;
import com.malty.apimanage.service.ApiMetaDataService;
import com.malty.apimanage.dao.ApiMetaDataMapper;
import com.malty.common.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【api_meta_data】的数据库操作Service实现
 * @createDate 2025-09-26 17:09:00
 */
@Service
public class ApiMetaDataServiceImpl extends ServiceImpl<ApiMetaDataMapper, ApiMetaData>
        implements ApiMetaDataService {
    @Override
    public void saveApiMetaData(ApiAddReq apiAddReq) {
        ApiMetaData userInfo = toApiMetaData(apiAddReq);
        this.save(userInfo);
    }

    @Override
    public boolean updateApiMetaDataById(ApiUpdateReq apiUpdateRequest) {
        ApiMetaData apiMetaData = new ApiMetaData();
        BeanUtils.copyProperties(apiUpdateRequest, apiMetaData);
        return this.updateById(apiMetaData);
    }

    @Override
    public ApiMetaDataVO getApiMetaDataById(Long id) {
        ApiMetaData apiMetaData = this.getById(id);
        return toApiMetaDataVO(apiMetaData);
    }

    @Override
    public List<ApiMetaDataVO> listApiMetaDataByQuery(ApiQueryReq apiQueryReq) {
        ApiMetaData userQuery = new ApiMetaData();
        if (apiQueryReq != null) {
            BeanUtils.copyProperties(apiQueryReq, userQuery);
        }

        QueryWrapper<ApiMetaData> queryWrapper = new QueryWrapper<>(userQuery);
        List<ApiMetaData> userList = this.list(queryWrapper);

        return userList.stream()
                .map(this::toApiMetaDataVO)
                .collect(Collectors.toList());
    }

    @Override
    public Page<ApiMetaDataVO> pageList(ApiQueryReq apiQueryReq) {
        long current = 1;
        long size = 10;
        ApiMetaData userQuery = new ApiMetaData();
        if (apiQueryReq != null) {
            BeanUtils.copyProperties(apiQueryReq, userQuery);
            current = apiQueryReq.getCurrent();
            size = apiQueryReq.getPageSize();
        }

        QueryWrapper<ApiMetaData> queryWrapper = new QueryWrapper<>(userQuery);
        Page<ApiMetaData> userPage = this.page(new Page<>(current, size), queryWrapper);

        Page<ApiMetaDataVO> dataVOPage = new Page<>(userPage.getCurrent(), userPage.getSize(), userPage.getTotal());

        List<ApiMetaDataVO> userVOList = userPage.getRecords().stream()
                .map(this::toApiMetaDataVO)
                .collect(Collectors.toList());
        dataVOPage.setRecords(userVOList);
        return dataVOPage;
    }

    @Override
    public Boolean updateStatus(Long id, int status) {
        ApiMetaData apiMetaData = this.getById(id);
        if (apiMetaData.getStatus() == status) {
            throw new BusinessException(ErrorCode.REPEATED_ERROR, "状态已更新，勿重复操作");
        }
        apiMetaData.setStatus(status);
        return this.updateById(apiMetaData);
    }

    @Override
    public Boolean apiOnline(Long id) {
        ApiMetaData apiMetaData = getById(id);
        if (apiMetaData == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "接口不存在");
        }
        // todo 判断接口是否可以调用

        // 更新
        return updateStatus(id, 1);
    }

    @Override
    public Boolean apiOffline(Long id) {
        ApiMetaData apiMetaData = getById(id);
        if (apiMetaData == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "接口不存在");
        }
        return updateStatus(id, 1);
    }

    private ApiMetaData toApiMetaData(ApiAddReq apiAddReq) {
        ApiMetaData metaDataVO = new ApiMetaData();
        BeanUtils.copyProperties(apiAddReq, metaDataVO);
        return metaDataVO;
    }

    private ApiMetaDataVO toApiMetaDataVO(ApiMetaData apiMetaData) {
        ApiMetaDataVO metaDataVO = new ApiMetaDataVO();
        BeanUtils.copyProperties(apiMetaData, metaDataVO);
        return metaDataVO;
    }
}




